using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;

namespace IGGConsole
{
    class StringFormatter
    {
        private static CultureInfo m_encaInfo = new CultureInfo("en-ca");

        /// <summary>
        /// Write to the console a graphic box structure
        /// </summary>
        /// <param name="top">If it is the top of the box to write</param>
        /// <param name="num">The number of boxes</param>
        public static void WriteGraphicBoxStructure(bool top, int num)
        {
            for (int i = 0; i < num; i++)
            {
                if (top)
                {
                    WriteTopBox();
                }
                else
                {
                    WriteBottomBox();
                }
                if (i + 1 == num)
                {
                    Console.WriteLine();
                }
            }
        }

        private static void WriteTopBox()
        {
            Console.Write((char)218);
            for (int i = 0; i < 10; i++)
            {
                Console.Write((char)196);
            }
            Console.Write((char)191);
        }

        private static void WriteBottomBox()
        {
            Console.Write((char)192);
            for (int i = 0; i < 10; i++)
            {
                Console.Write((char)196);
            }
            Console.Write((char)217);
        }



        /// <summary>
        /// Write a name in the middle of the console
        /// </summary>
        public static void WriteInMiddle(string name)
        {
            int middle = TxtConstants.COMMANDLINE_CHAR_WIDTH / 2;
            int start = middle - (name.Length / 2);
            Console.WriteLine(name.PadLeft(start + name.Length, ' '));
        }

        public static void WriteShortstop(string name)
        {
            int quarter = TxtConstants.COMMANDLINE_CHAR_WIDTH / 4;
            int start = quarter - (name.Length / 2);
            Console.WriteLine(name.PadLeft(start + name.Length, ' '));
        }

        /// <summary>
        /// Format a long text for the console
        /// </summary>
        public static void FormatLongText(string longText)
        {
            string[] words = longText.Split(new char[] { ' ' });
            for (int i = 0; i < words.Length; i++)
            {
                string sentence = words[i];
                bool continued = true;
                while (continued)
                {
                    if ((i + 1) < words.Length)
                    {
                        if ((sentence.Length + 1 + words[i + 1].Length) < 75)
                        {
                            sentence += " " + words[++i];
                        }
                        else
                        {
                            continued = false;
                        }
                    }
                    else
                    {
                        continued = false;
                    }
                }

                Console.WriteLine(" " + sentence);
            }
        }

        public static void WriteInfieldLine(string third, string pitcher, string first)
        {
            int middle = TxtConstants.COMMANDLINE_CHAR_WIDTH / 2;
            int start = middle - (pitcher.Length / 2);
            int firstpad = start - (TxtConstants.COMMANDLINE_FIELD_PADDING + third.Length);
            int secondpad = TxtConstants.COMMANDLINE_CHAR_WIDTH 
                - (TxtConstants.COMMANDLINE_FIELD_PADDING + first.Length) 
                - (start + pitcher.Length);

            Console.Write(third.PadLeft(TxtConstants.COMMANDLINE_FIELD_PADDING + third.Length, ' '));
            Console.Write(pitcher.PadLeft(firstpad + pitcher.Length, ' '));
            Console.Write(first.PadLeft(secondpad + first.Length, ' '));
            Console.WriteLine();
        }

        /// <summary>
        /// Write the defensive outfield
        /// </summary>
        public static void WriteOutfield(string lf, string cf, string rf)
        {
            WriteInMiddle(cf);
            Console.WriteLine();

            int len = rf.Length + TxtConstants.COMMANDLINE_FIELD_PADDING;
            int padleft = lf.Length + TxtConstants.COMMANDLINE_FIELD_PADDING;
            int centralpad = TxtConstants.COMMANDLINE_CHAR_WIDTH - len - padleft;

            Console.WriteLine(lf.PadLeft(TxtConstants.COMMANDLINE_FIELD_PADDING + lf.Length, ' ') 
                + rf.PadLeft(centralpad + rf.Length, ' '));
        }

    }
}
